/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.core.rtgen.managergenerator;

import com.inspur.edp.bff.designtime.generator.core.rtgen.managergenerator.assembler.*;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGeneratorContext;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.dataextendinfo.VoDataExtendInfo;

import java.util.ArrayList;

public class AssemblerGenerator extends CompositeGenerator {
    private FSMgrGeneratorContext mgrContext;


    private GspViewModel vo;

    public AssemblerGenerator(FSMgrGeneratorContext mgrContext) {
        this.mgrContext = mgrContext;
        vo = mgrContext.getFSGeneratorContext().getVmGenContext().getVm();
    }


    @Override
    protected CompositeGeneratorContext createCompositeContext() {
        return new CompositeGeneratorContext();
    }

    @Override
    protected ArrayList<BaseGenerator> createCompositeItemGenerators() {

        VoDataExtendInfo extendInfo = vo.getDataExtendInfo();
        ArrayList<BaseGenerator> compositeItems = new ArrayList<BaseGenerator>();

        String filterCondition = vo.getMainObject().getFilterCondition();
        String sortCondition = vo.getMainObject().getOrderbyCondition();
        boolean hasCondition = !(filterCondition==null||filterCondition.equals("")) || !(sortCondition==null||sortCondition.equals(""));
        if(extendInfo.getBeforeQueryActions().size() > 0 || extendInfo.getQueryActions().size() > 0
                || extendInfo.getAfterQueryActions().size() > 0
                || hasCondition){
            QueryAssemblerGenerator queryAssembler = new QueryAssemblerGenerator(
                    mgrContext,
                    extendInfo.getBeforeQueryActions(),
                    extendInfo.getQueryActions(),
                    extendInfo.getAfterQueryActions());
            compositeItems.add(queryAssembler);
        }

        if(extendInfo.getDataMappingActions().size() > 0 || extendInfo.getDataReversalMappingActions().size() > 0){
            DataMapperAssemblerGenerator dataMapAssembler = new DataMapperAssemblerGenerator(
                    mgrContext,
                    extendInfo.getDataMappingActions(),
                    extendInfo.getDataReversalMappingActions());
            compositeItems.add(dataMapAssembler);
        }

        if(extendInfo.getBeforeRetrieveActions().size() > 0 || extendInfo.getRetrieveActions().size() > 0
                || extendInfo.getAfterRetrieveActions().size() > 0){
            RetrieveAssemblerGenerator retrieveAssembler = new RetrieveAssemblerGenerator(
                    mgrContext,
                    extendInfo.getBeforeRetrieveActions(),
                    extendInfo.getRetrieveActions(),
                    extendInfo.getAfterRetrieveActions());
            compositeItems.add(retrieveAssembler);
        }


        if(extendInfo.getBeforeCreateActions().size() > 0 || extendInfo.getCreateActions().size() > 0
                || extendInfo.getAfterCreateActions().size() > 0){
            RetrieveDefaultAssemblerGenerator createAssembler = new RetrieveDefaultAssemblerGenerator(
                    mgrContext,
                    extendInfo.getBeforeCreateActions(),
                    extendInfo.getCreateActions(),
                    extendInfo.getAfterCreateActions());
            compositeItems.add(createAssembler);
        }

        if(extendInfo.getBeforeModifyActions().size() > 0 || extendInfo.getModifyActions().size() > 0
                || extendInfo.getAfterModifyActions().size() > 0){
            ModifyAssemblerGenerator modifyAssembler = new ModifyAssemblerGenerator(
                    mgrContext,
                    extendInfo.getBeforeModifyActions(),
                    extendInfo.getModifyActions(),
                    extendInfo.getAfterModifyActions());
            compositeItems.add(modifyAssembler);
        }

        if(extendInfo.getBeforeDeleteActions().size() > 0 || extendInfo.getDeleteActions().size()> 0
                || extendInfo.getAfterDeleteActions().size() > 0 || extendInfo.getBeforeMultiDeleteActions().size() > 0
                || extendInfo.getMultiDeleteActions().size() > 0 || extendInfo.getAfterMultiDeleteActions().size() > 0){
            DeleteAssemblerGenerator delAssembler = new DeleteAssemblerGenerator(
                    mgrContext,
                    extendInfo.getBeforeDeleteActions(),
                    extendInfo.getDeleteActions(),
                    extendInfo.getAfterDeleteActions(),
                    extendInfo.getBeforeMultiDeleteActions(),
                    extendInfo.getMultiDeleteActions(),
                    extendInfo.getAfterMultiDeleteActions());
            compositeItems.add(delAssembler);
        }


        if(extendInfo.getBeforeSaveActions().size() > 0 || extendInfo.getAfterSaveActions().size() > 0){
            SaveAssemblerGenerator saveAssembler = new SaveAssemblerGenerator(
                    mgrContext,
                    extendInfo.getBeforeSaveActions(),
                    extendInfo.getAfterSaveActions());
            compositeItems.add(saveAssembler);
        }

        if(extendInfo.getChangesetMappingActions().size() > 0 || extendInfo.getChangesetReversalMappingActions().size() > 0){
            ChangeMapAssemblerGenerator changeMapGenerator = new ChangeMapAssemblerGenerator(
                    mgrContext,
                    extendInfo.getChangesetMappingActions(),
                    extendInfo.getChangesetReversalMappingActions());
            compositeItems.add(changeMapGenerator);
        }


        //TODO 当前还没有消息映射方法
//        MessageMapAssemblerGenerator msgMapGenerator = new MessageMapAssemblerGenerator(mgrContext, msgMapperTypes);
//        compositeItems.add(msgMapGenerator);

        return compositeItems;
    }
}
